﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace PipeTest.Script
{
    public class ScriptExecutor
    {
        private ScriptContext scriptContext;
        private bool shouldContinue;

        public ScriptExecutor(ScriptContext scriptContext)
        {
            this.scriptContext = scriptContext;
        }

        public void Execute(object state)
        {
            shouldContinue = true;
            do
            {
                ExecuteAction(scriptContext.CurrentAction);
            } while (!scriptContext.IsEnd() && shouldContinue);
        }

        public void Stop()
        {
            shouldContinue = false;
        }

        private void ExecuteAction(ScriptAction action)
        {
            scriptContext.CurrentActionIndex++;
            switch (action.ActionType)
            {
                case ActionType.BringupWindow:
                    scriptContext.GameVistor.BringupWindow();
                    break;
                case ActionType.GoTo:
                    scriptContext.CurrentActionIndex= scriptContext.FindScriptActionByName(action.Parameter);
                    break;
                case ActionType.SendKey:
                    scriptContext.GameVistor.SendKey(action.Parameter[0]);
                    break;
                case ActionType.UnKnown:
                    break;
                case ActionType.Wait:
                    Thread.Sleep(int.Parse(action.Parameter));
                    break;   
            }
        }
    }
}
